# iRotorSerial.tcl: A program to link SatPC32's Yaesu G232 rotor driver 
# with AA2TX's parallel port iRotor interface and related software.
# This software requires a null-modem connection between the serial port
# stated in SatPC32's driver and the one indicated below. You can use the 
# opensource and free com0com package to create this pair. 

#The following variables are user defined:

# name of the serial port. Note the escaped slashes that must be used.
# Alter only the last five characters unless you really know what you're doing

set serialPort "\\\\.\\CNCB0"

# The speed of the serial connection, set in SatPC32's control. Use 57600
# unless there are difficulties with this.
set serialSpeed "57600"

# CHANGE the values below to suit your circumstances:
# The path to the directory containing AA2TX's 'ROTORDRV.EXE and 'ROTOR.EXE'
set iRotorDirectory "D://Documents and Settings/Bruce/Desktop/Rotordrv/"

# The following four are parameters passed directly to 'ROTORDRV'.
# See AA2TX's software notes for futher information

# The parallel port to which the rotor controllers are connected
set rotorParallelPortNumber 1
# Is the azimuth stop at the south 'S' or north 'N'?
set stopPole "S"
# Should this program control the azimuth only 'AzOnly' or 'Both' rotors
set rotorMode "Both"

# Number of minutes of no rotor commands until the program moves the rotor
# to a parking position
set parkingTimeoutMinutes "10"

# elevation and azimuth of rotor when in parking position. 
# Make these the same value as used
# in SatPC32's parking configuration
set parkingElevation "0"
set parkingAzimuth "260"

# DO NOT alter variables after this point

# Open the serial port
set serial [open $serialPort r+]

# Configure the serial port
fconfigure $serial -mode "57600,n,8,1"

# With blocking set to '1' it seems a CTRL-C does not kill the command-line 
# program, so I'm using this approach
fconfigure $serial -blocking 1 -buffering none

# setup timer for parking timeout
set timeoutSeconds [expr $parkingTimeoutMinutes * 60]
puts "timeoutseconds  $timeoutSeconds"
set timer [clock seconds]
puts $timer
set timeoutTime [expr $timer + $timeoutSeconds]
# The main loop to read from the serial input line
while {1} {
    if ([expr [clock seconds] > $timeoutTime]) {
	puts "Parking ..."
	set timer [clock seconds]
	set timeoutTime [expr $timer + $timeoutSeconds]
    }
     gets $serial input
     # match a string of the form "W123 123", which is the sort of 
     # data the Yaesu driver gives
     # store the first three digits in $az and the last three in $elev
     # for further use.
     if {[regexp {W(\d\d\d) (\d\d\d)} $input output az elev]} {
   puts "az  $az"
   puts "el  $elev"
 } 
}
